class EnemyHealer extends EnemyCharacter
{
	//References
	private var _healRadius:int = 3;
	private var _healPercent:int = 40;
	private var healSound:AudioClip;		
	
	public function EnemyHealer()
	{
		toDisplayOverlayText = false;
				
		turnsLeft = MAX_TURNS;
		
		isExecuting = false;
	
		_visible = true;
		_hasMoved = true;
		_path = new Array(0);
		_destReached = true;
		_destTile = null;
		characterName = "Enemy";
		
		//Attack
		attackRadius = 4;
		attackDamage = 15;
		actualDamage = 15;
		_attackPercent = 50.0;
		skillRadius = 2;
		
		//Sight
		sightRadius = 7;
		moveRadius = 6;
		
		_isSelectable = false;
		_attackableText = null;
		
		//Health Status
		maxHealth = 100;
		myHealth = maxHealth;
		
		//For diplaying mouse over
		healthPercent = 100.0f;
		healthBarLength = Screen.width/9;
		style = new GUIStyle();
	    texture = new Texture2D(1, 1);
		
		//Enemy type
		_enemyType = EnemyType.ENEMY_HEALER;
		
		_isSelectable = false;
		_attackableText = null;
		
		healSound = Resources.Load("healSound");
	}
	
	public function attack(target:GameCharacter, projectileType:int)
	{
		super.attack(target, projectileType);
		
		//calcualte hit chance
		var hitChance = calculateHitChance(target);		
		
		//play attack animation
		setAnimationState(ATTACK_ANI);
		
		//fire bullet
		var bullet = Resources.Load("Projectile_firstAidKit");
		bullet = Instantiate(bullet);
		var projectile = bullet.GetComponent(Projectile);
		
		//random attack chance
		if (Random.Range(0,100) < hitChance)
			//hit
			projectile.setup(this, target, true, projectileType);
		else
			//miss
			projectile.setup(this, target, false, projectileType);
			
		Destroy(bullet,5);
		isLocked = false;
		if (projectileType == Projectile.NORMAL)
			endTurn();
	}
	
	//OVERRIDE THIS FUNCTION IN EVERY INHERITED CLASS
	public function calculateHitChance(target:GameCharacter)
	{
		//currently calculating based on an average atk
		//calculate distance of enemy
		var distance = Vector3.Distance(target.getSurfaceCoord(), this.getSurfaceCoord());
		//based on the maximum range of weapon, calculate the hit chance based on the optimized range
		//close - mid range weapon
		var optimizedRangeMax = 5;
		var hitChance = 100;
		//too far
		if (distance > optimizedRangeMax)
			hitChance -= (distance - optimizedRangeMax) * 20;
		//if in cover
		if (target.isInCover())
		{
			if (!target.isFlanked(this))
				hitChance -= Obstacle.HIT_CHANCE_REDUCTION;
		}
		
		//check height advantage
		var heightDifference = target.getSurfaceCoord().y - this.getSurfaceCoord().y;
		if (heightDifference > 0)
			hitChance -= heightDifference * 10;
		else if (heightDifference < 0)
			hitChance += heightDifference * -10;
		
		//minimum 10%
		if (hitChance < 10)
			hitChance = 10;
		else if (hitChance > 100)
			hitChance = 100;
		return hitChance;
	}
	
	public function calculateActualDamage(target:GameCharacter)
	{
		//if in cover
		if (target.isInCover())
		{
			//check whether flanked
			if (target.isFlanked(this))
			{
				return attackDamage *2; 
			}
			else
			{
				return attackDamage;
			}
		}
		//not in cover
		else
		{
			return attackDamage *2;
		}
	}
	
	public function skill(target:EnemyCharacter) // Heal ally
	{
		if(!_isCooldown){
			setAnimationState(SKILL_ANI);
			startCooldown();
			
			var directionOfAttack:Vector3 = target.getSurfaceCoord() - this.getSurfaceCoord();
			directionOfAttack.y = 0;
			this.transform.rotation = Quaternion.LookRotation(directionOfAttack);
			
			audio.PlayClipAtPoint(healSound,Vector3(0,0,0), 0.5f);
			
			Debug.Log("HEAAAALINGGG");
			target.increaseHealthPercent(_healPercent);
			
			endTurn();
		}
	}
	
	public function getHealRadius() { return _healRadius; }
	
}